package com.zbcbms.controller;

import com.alibaba.druid.util.StringUtils;
import com.zbcbms.common.AlertUtilCommon;
import com.zbcbms.entity.Book;
import com.zbcbms.entity.tableData.BookTableData;
import com.zbcbms.service.impl.BookServiceImpl;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.Alert;
import javafx.scene.control.ComboBox;
import javafx.scene.control.DatePicker;
import javafx.scene.control.TextField;
import javafx.scene.layout.AnchorPane;
import java.math.BigDecimal;
import java.net.URL;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.ResourceBundle;

/**
 * @author fengq
 */
public class UpdateBookController implements Initializable {
    @FXML
    private TextField updateBookNumber;

    @FXML
    private TextField updateBookName;

    @FXML
    private TextField updateBookAuthor;

    @FXML
    private TextField updatePublishingHouse;

    @FXML
    private DatePicker updatePublishingTime;

    @FXML
    private TextField updatePrice;

    @FXML
    private TextField updateType;

    @FXML
    private ComboBox<String> updateStatus;

    @FXML
    private AnchorPane updateBookAnchorPane;

    @Override
    public void initialize(URL location, ResourceBundle resources) {
        List<String> snackList = Arrays.asList("空闲", "使用中");
        ObservableList<String> obList = FXCollections.observableArrayList(snackList);
        updateStatus.setItems(obList);
    }

    @FXML
    public void updateBookAction(){
        String bookNumber = updateBookNumber.getText();
        String bookName = updateBookName.getText();
        String bookAuthor = updateBookAuthor.getText();
        String publishingHouse=updatePublishingHouse.getText();
        String publishingTime=updatePublishingTime.getEditor().getText();
        String price = updatePrice.getText();
        String type = updateType.getText();
        String status=updateStatus.getValue();
        if (StringUtils.isEmpty(bookNumber)||StringUtils.isEmpty(bookName)||StringUtils.isEmpty(price)||StringUtils.isEmpty(type)||StringUtils.isEmpty(bookAuthor)||StringUtils.isEmpty(status)){
            new AlertUtilCommon().AlterMessageInform(updateBookNumber.getScene().getWindow(), Alert.AlertType.ERROR,"错误提醒","关键参数不能空","修改失败");
        }else {
            if (StringUtils.isEmpty(publishingHouse)||publishingHouse==""){
                publishingHouse="null";
            }
            if (StringUtils.isEmpty(publishingTime)||publishingTime==""){
                publishingTime="null";
            }

            try {
                Book book = new Book();
                book.setBookNumber(bookNumber);
                book.setBookName(bookName);
                book.setAuthor(bookAuthor);
                book.setPublishingHouse(publishingHouse);
                book.setStatus(status);
                Date date = new SimpleDateFormat("yyyy-MM-dd").parse(publishingTime);
                book.setPublishingTime(date);
                book.setPrice(new BigDecimal(price));
                book.setType(type);
                Boolean aBoolean = new BookServiceImpl().updateById(book);
                if (aBoolean){
                    new AlertUtilCommon().AlterMessageInform(updateBookNumber.getScene().getWindow(), Alert.AlertType.INFORMATION,"修改提醒","修改成功","小仲书提示你:操作成功");
                    HomePageController homePageController = new HomePageController();
                    homePageController.setMainFrameAnchorPane((AnchorPane) updateBookAnchorPane.getParent());
                    homePageController.do_queryBookMenuItem_event(null);
                }else {
                    new AlertUtilCommon().AlterMessageInform(updateBookNumber.getScene().getWindow(), Alert.AlertType.INFORMATION,"修改提醒","修改错误","小仲书提示你:差忆点点成功");
                }
            } catch (ParseException e) {
                e.printStackTrace();
            }
        }
    }

    /**
     * 编辑数据渲染
     * @param bookTableData
     */
    public void updateTextInit(BookTableData bookTableData){
        updateBookNumber.setText(bookTableData.getBookNumber().get());
        updateBookName.setText(bookTableData.getBookName().get());
        updateBookAuthor.setText(bookTableData.getAuthor().get());
        updatePublishingHouse.setText(bookTableData.getPublishingHouse().get());
        updatePublishingTime.getEditor().setText(bookTableData.getPublishingTime().get());
        updatePrice.setText(bookTableData.getPrice().get());
        updateType.setText(bookTableData.getType().get());
        updateStatus.setValue(bookTableData.getStatus().getValue());
    }

}
